﻿using System;
using System.Data;
using System.Collections.Generic;
using Chenxin.Lib.Business.Entity;
using Chenxin.Lib.Business.DAL;

namespace Chenxin.Lib.Business.BLL
{
    /// <summary>
    /// 业务逻辑类CommissionRate 的摘要说明。
    /// </summary>
    public class CommissionRate
    {
        //Cache服务类
        private Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.DICTDATA);
        private readonly Chenxin.Lib.Business.DAL.CommissionRate dal = new Chenxin.Lib.Business.DAL.CommissionRate();
        public CommissionRate()
        { }
        #region  成员方法
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Chenxin.Lib.Business.Entity.CommissionRate GetModel(Guid RecordId)
        {

            return dal.GetModel(RecordId);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }

        public List<Chenxin.Lib.Business.Entity.CommissionRate> GetListByParams(Dictionary<string, string> dicParams)
        {
            
            List<Chenxin.Lib.Business.Entity.CommissionRate> lsRate;

            //查找Cache的用户权限键
            string strCacheKey = string.Format(Chenxin.Lib.Cache.CacheDataType.BUSINESS_STANDARDRATE, dicParams["CompanyId"], dicParams["BusinessType"], dicParams["HouseProperty"]);

            //从Cache中取得数据
            lsRate = cs.GetList<Chenxin.Lib.Business.Entity.CommissionRate>(strCacheKey);
            if (true)//!cs.Hit
            {
                System.Text.StringBuilder strWhere = new System.Text.StringBuilder();

                //公司ID
                strWhere.AppendFormat("CompanyId='{0}' ", dicParams["CompanyId"]);
                //交易类型
                strWhere.AppendFormat("And BusinessType={0} ", dicParams["BusinessType"]);
                //房屋性质
                strWhere.AppendFormat("And HouseProperty={0} ", dicParams["HouseProperty"]);

                lsRate =  GetModelList(strWhere.ToString());

                if (lsRate != null && lsRate.Count>0) cs.Set(strCacheKey, lsRate);
            }

            return lsRate;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.Business.Entity.CommissionRate> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.Business.Entity.CommissionRate> DataTableToList(DataTable dt)
        {
            List<Chenxin.Lib.Business.Entity.CommissionRate> modelList = new List<Chenxin.Lib.Business.Entity.CommissionRate>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Chenxin.Lib.Business.Entity.CommissionRate model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Chenxin.Lib.Business.Entity.CommissionRate();
                    if (dt.Rows[n]["RecordId"].ToString() != "")
                    {
                        model.RecordId = new Guid(dt.Rows[n]["RecordId"].ToString());
                    }
                    if (dt.Rows[n]["CityId"].ToString() != "")
                    {
                        model.CityId = new Guid(dt.Rows[n]["CityId"].ToString());
                    }
                    model.CityName = dt.Rows[n]["CityName"].ToString();
                    if (dt.Rows[n]["CompanyId"].ToString() != "")
                    {
                        model.CompanyId = new Guid(dt.Rows[n]["CompanyId"].ToString());
                    }
                    model.CompanyName = dt.Rows[n]["CompanyName"].ToString();
                    if (dt.Rows[n]["BusinessType"].ToString() != "")
                    {
                        model.BusinessType = int.Parse(dt.Rows[n]["BusinessType"].ToString());
                    }
                    if (dt.Rows[n]["HouseProperty"].ToString() != "")
                    {
                        model.HouseProperty = int.Parse(dt.Rows[n]["HouseProperty"].ToString());
                    }
                    if (dt.Rows[n]["VStandardRate"].ToString() != "")
                    {
                        model.VStandardRate = decimal.Parse(dt.Rows[n]["VStandardRate"].ToString());
                    }
                    if (dt.Rows[n]["BStandardRate"].ToString() != "")
                    {
                        model.BStandardRate = decimal.Parse(dt.Rows[n]["BStandardRate"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }
        #endregion  成员方法
    }
}
